﻿Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Drawing.Design
Imports System.Workflow.Activities.Rules.Design
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design
Imports System.Workflow.ComponentModel.Serialization

Namespace System.Workflow.Activities.Rules
    <Browsable(True), TypeConverter(GetType(RuleSetReferenceTypeConverter)), DesignerSerializer(GetType(WorkflowMarkupSerializer), GetType(WorkflowMarkupSerializer)), DesignerSerializer(GetType(DependencyObjectCodeDomSerializer), GetType(CodeDomSerializer)), ActivityValidator(GetType(RuleSetReferenceValidator)), Editor(GetType(RuleSetNameEditor), GetType(UITypeEditor))> _
    Public NotInheritable Class RuleSetReference
        Inherits DependencyObject
        ' Methods
        Public Sub New()
            Me.syncLock = New Object
        End Sub

        Public Sub New(ByVal ruleSetName As String)
            Me.syncLock = New Object
            Me._name = ruleSetName
        End Sub

        Protected Overrides Sub InitializeProperties()
            SyncLock Me.syncLock
                If Not Me._runtimeInitialized Then
                    Dim parentDependencyObject As Activity = [TryCast](MyBase.ParentDependencyObject,Activity)
                    Dim declaringActivity As CompositeActivity = Helpers.GetDeclaringActivity(parentDependencyObject)
                    If (declaringActivity Is Nothing) Then
                        declaringActivity = [TryCast](Helpers.GetRootActivity(parentDependencyObject),CompositeActivity)
                    End If
                    Dim definitions As RuleDefinitions = ConditionHelper.Load_Rules_RT(declaringActivity)
                    If (Not definitions Is Nothing) Then
                        definitions.OnRuntimeInitialized
                    End If
                    MyBase.InitializeProperties
                    Me._runtimeInitialized = True
                End If
            End SyncLock
        End Sub


        ' Properties
        Public Property RuleSetName As String
            Get
                Return Me._name
            End Get
            Set(ByVal value As String)
                Me._name = value
            End Set
        End Property


        ' Fields
        Private _name As String
        Private _runtimeInitialized As Boolean
        <NonSerialized> _
        Private [syncLock] As Object
    End Class
End Namespace

